package com.nf.demo.dao.impl;

import com.nf.demo.dao.StaffsDAO;
import com.nf.demo.model.pojo.Staffs;
import com.nf.demo.utils.CustomBeanProcessor;
import com.nf.demo.utils.DruidUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lwj
 * @date 2025-02-26
 * @vesion 1.0.0
 */
public class StaffsDAOImpl4DbUtils implements StaffsDAO {
    QueryRunner run = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public ArrayList<Staffs> selectAll() {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";

        // 创建自定义 RowProcessor
        RowProcessor rowProcessor = new BasicRowProcessor(new CustomBeanProcessor());
        try{
            return (ArrayList<Staffs>) run.query(sql,new BeanListHandler<Staffs>(Staffs.class,rowProcessor));
        } catch (SQLException e) {
            throw new RuntimeException("获取数据失败",e);
        }
    }

    @Override
    public Integer delete(Staffs staffs) {
        // 定义 SQL 语句
        String sql = "delete from staffs where id = ?";
        try{
            return run.update(sql,staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException("删除失败",e);
        }
    }

    @Override
    public Integer update(Staffs staffs) {
        // 定义 SQL 语句
        String sql = "update staffs set name = ?, age = ?, phone = ?, sta_pos = ?,update_time = ? where id = ?";
        try{
            return run.update(sql,staffs.getName(),staffs.getAge(),staffs.getPhone(),staffs.getStaPos(),staffs.getUpdateTime(),staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException("修改失败",e);
        }
    }

    @Override
    public Integer insert(Staffs staffs) {
        // 1.定义要操作的 SQL 语句
        String sql = "insert into staffs(name,age,phone,sta_pos) values (?,?,?,?);";

        // 3.执行s SQL 语句
        try {
            // 不定长参数本质上是数组
            return run.update(sql,staffs.getName(),staffs.getAge(),staffs.getPhone(),staffs.getStaPos());
        } catch (SQLException e) {
            throw new RuntimeException("添加失败",e);
        }
    }

    @Override
    public List<Staffs> selectPage(int page, int totalRow) {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs limit ?,?";

        // 创建自定义 RowProcessor
        RowProcessor rowProcessor = new BasicRowProcessor(new CustomBeanProcessor());
        try{
            return run.query(sql,new BeanListHandler<>(Staffs.class,rowProcessor),(page-1)*totalRow,totalRow);
        } catch (SQLException e) {
            throw new RuntimeException("分页数组信息失败",e);
        }
    }

    @Override
    public Staffs selectById(int id) {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";

        // 创建自定义 RowProcessor
        RowProcessor rowProcessor = new BasicRowProcessor(new CustomBeanProcessor());
        try{
            return run.query(sql,new BeanHandler<>(Staffs.class,rowProcessor),id);
        } catch (SQLException e) {
            throw new RuntimeException("根据编号查询失败",e);
        }
    }
}
